//
//  MeTabBar.m
//  MeLive
//
//  Created by liulei on 2017/5/10.
//  Copyright © 2017年 liulei. All rights reserved.
//

#import "MeTabBar.h"
#import "MePublishBtnView.h"


#define kTabBarItemCount 3

@interface MeTabBar ()<MePublishBtnViewDelegate>

@property (nonatomic, strong) MePublishBtnView *publishBtnView;/**< 发布按钮 */

@end

@implementation MeTabBar

#pragma mark - life cycle
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger count = kTabBarItemCount;
    [self addSubview:self.publishBtnView];
    // 1.设置加号按钮的位置
    CGFloat midBtnViewWidth = self.width / count;
    CGFloat midBtnViewHeight = self.height;
    CGFloat midBtnViewX = (self.width - midBtnViewWidth) * 0.5;
    CGFloat midBtnViewY = 0;
    self.publishBtnView.frame = CGRectMake(midBtnViewX, midBtnViewY, midBtnViewWidth, midBtnViewHeight);
    
    // 2.设置其他tabbarButton的frame
    CGFloat tabBarButtonW = self.width / count;
    CGFloat tabBarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            // 设置x
            child.x = tabBarButtonIndex * tabBarButtonW;
            // 设置宽度
            child.width = tabBarButtonW;
            // 增加索引
            tabBarButtonIndex++;
            if (tabBarButtonIndex == count - 2) {
                tabBarButtonIndex++;
            }
        }
    }
}
#pragma mark - delegate
#pragma mark - event response
/**
 *  加号按钮点击
 */
- (void)didClickPublishBtnInMePublishBtnView:(MePublishBtnView *)publishBtnView
{
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
        [self.customDelegate tabBarDidClickPlusButton:self];
    }
}
#pragma mark - private methods
#pragma mark - getters and setters
- (MePublishBtnView *)publishBtnView
{
    if (_publishBtnView == nil) {
        _publishBtnView = [[MePublishBtnView alloc] init];
        _publishBtnView.delegate = self;
    }
    return _publishBtnView;
}

@end
